﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary.ItemClasses;
using RpgLibrary;


namespace RpgEditor
{
    public partial class FormWeaponDetails : FormDetailBase, IDetailForm
    {
       
        #region Field Region
        
        WeaponData weapon = null;
        
        #endregion
        
        #region Property Region
        
        public WeaponData Weapon
        {
            get { return weapon; }
            set { weapon = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)weapon;
            }
            set
            {
                weapon = (WeaponData)value;
            }
        }

        #endregion
        
        #region Constructor Region
        
        public FormWeaponDetails()
        {
            InitializeComponent();

            SetDataObjectValueFunction = SetWeaponValue;
            LoadDataObjectValueFunction = LoadWeaponValue;
        }
        
        #endregion
        
        #region Event Handler Region

        void btnAddAllowed_Click(object sender, EventArgs e)
        {
            while (lbAvailableClasses.SelectedItems.Count > 0)
            {
                lbAllowedClasses.Items.Add(lbAvailableClasses.SelectedItems[0]);
                lbAvailableClasses.Items.Remove(lbAvailableClasses.SelectedItems[0]);
            }
        }

        void btnRemoveAllowed_Click(object sender, EventArgs e)
        {
            while (lbAllowedClasses.SelectedItems.Count > 0)
            {
                lbAvailableClasses.Items.Add(lbAllowedClasses.SelectedItems[0]);
                lbAllowedClasses.Items.Remove(lbAllowedClasses.SelectedItems[0]);
            }
        }

        void LoadWeaponValue()
        {
            foreach (string s in FormMain.EntityDataManager.CharacterData.Keys)
                lbAvailableClasses.Items.Add(s);
            
            foreach (Hands location in Enum.GetValues(typeof(Hands)))
                cbHands.Items.Add(location);
            cbHands.SelectedIndex = 0;

            foreach (DieType die in Enum.GetValues(typeof(DieType)))
                cboDieType.Items.Add(die);
            cboDieType.SelectedIndex = 0;
            cboDieType.SelectedValue = Enum.GetName(typeof(DieType), DieType.D4);

            if (weapon != null)
            {
                tbName.Text = weapon.Name;
                tbType.Text = weapon.Type;
                nudPrice.Value = weapon.Price;
                nudWeight.Value = (decimal)weapon.Weight;
                cbHands.SelectedIndex = (int)weapon.NumberHands;
                nudAttackValue.Value = weapon.AttackValue;
                nudAttackModifier.Value = weapon.AttackModifier;
                nudNumberOfDice.Value = weapon.DamageEffectData.NumberOfDice; ;
                nudDamageModifier.Value = weapon.DamageEffectData.Modifier;

                for (int i = 0, cnt = cboDieType.Items.Count; i < cnt; i++)
                {
                    if (cboDieType.Items[i].ToString() ==
                        weapon.DamageEffectData.DieType.ToString())
                    {
                        cboDieType.SelectedIndex = i;
                        cboDieType.SelectedValue = cboDieType.Items[i];
                        break;
                    }
                }

                foreach (string s in weapon.AllowableClasses)
                {
                    if (lbAvailableClasses.Items.Contains(s))
                        lbAvailableClasses.Items.Remove(s);
                    
                    lbAllowedClasses.Items.Add(s);
                }
            }
        }

        bool SetWeaponValue()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("You must enter a name for the item.");
                return false;
            }
            
            List<string> allowedClasses = new List<string>();
            foreach (object o in lbAllowedClasses.Items)
                allowedClasses.Add(o.ToString());
            
            weapon = new WeaponData();
            weapon.Name = tbName.Text;
            weapon.Type = tbType.Text;
            weapon.Price = (int)nudPrice.Value;
            weapon.Weight = (float)nudWeight.Value;
            weapon.AttackValue = (int)nudAttackValue.Value;
            weapon.AttackModifier = (int)nudAttackModifier.Value;
            weapon.AllowableClasses = allowedClasses.ToArray();

            weapon.DamageEffectData.Name = tbName.Text;
            weapon.DamageEffectData.AttackType = RpgLibrary.EffectClasses.AttackType.Health;
            weapon.DamageEffectData.DamageType = RpgLibrary.EffectClasses.DamageType.Weapon;
            weapon.DamageEffectData.DieType = (DieType)Enum.Parse(
                typeof(DieType),
                cboDieType.Items[cboDieType.SelectedIndex].ToString());
            weapon.DamageEffectData.NumberOfDice = (int)nudNumberOfDice.Value;
            weapon.DamageEffectData.Modifier = (int)nudDamageModifier.Value;

            return true;
        }
        
        #endregion
    }
}